function validate_onlyChars(e, classname) {
	var e = window.event || e;
	var keyunicode = e.charCode || e.keyCode;

	if (!(keyunicode >= 65 && keyunicode <= 122 || keyunicode == 8 || keyunicode == 32 )) {

		$("#f_regist ." + classname + " .message").text("Não são permitidos números...");
		$("#f_regist ." + classname + " .message").css({
			'color' : 'red'
		});
	} else {
		$("#f_regist ." + classname + " .message").text("*obrigatório");
		$("#f_regist ." + classname + " .message").css({
			'color' : 'red'
		});
	}
	return (keyunicode >= 65 && keyunicode <= 122 || keyunicode == 8 || keyunicode == 32) ? true : false;
}


$(window).load(function() {
	var isEmpty = true;
	var isSmall = true;
	
	$("#regist #f_regist #ip_name").keypress(function(e) {
		var classname = $(this).closest('tr').attr('class');

		return validate_onlyChars(e, classname);
	});
	$("#regist #f_regist #ip_name").blur(function() {
		var classname = $(this).closest('tr').attr('class');
		var length = $(this).val().length;
		if (length < 5) {
			isSmall = true;
			$("#f_regist ." + classname + " .message").text("*Introduza o nome completo...");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
		} else {
			isSmall = false;
			$("#f_regist ." + classname + " .message").text("*obrigatório");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
		}
	});

	$("#regist #f_regist #ip_mail").blur(function() {
		var classname = $(this).closest('tr').attr("class");
		var length = $(this).val().length;
		var x = $(this).val();
		var atpos = x.indexOf("@");
		var dotpos = x.lastIndexOf(".");
		if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
			$("#f_regist ." + classname + " .message").text("E-mail não válido!");
			$(this).animate({
				backgroundColor : 'red'
			}, 500);
			return false;
		} else {
			$("#f_regist ." + classname + " .message").text("OK!");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'green'
			});
			$(this).animate({
				backgroundColor : 'none'
			}, 500);
		}
		if (length < 9) {
			isSmall = true;
			$("#f_regist ." + classname + " .message").text("*O seu mail é demasiado pequeno...");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
		} else {
			isSmall = false;
			$("#f_regist ." + classname + " .message").text("*obrigatório");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
		}
	});
	$("#regist #f_regist #ip_contact").keypress(function(e) {

		var classname = $(this).closest('tr').attr("class");

		var theEvent = e || window.event;
		var key = theEvent.keyCode || theEvent.which;

		key = String.fromCharCode(key);

		if (theEvent.keyCode === 8) {
			return;
		}
		if (theEvent.keyCode === 9) {
			return;
		}
		if (key === "+") {
			return;
		}
		var regex = /[0-9]|\./;
		if (!regex.test(key)) {
			$("#f_regist ." + classname + " .message").text("*Não são permitidas letras");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
			theEvent.returnValue = false;
			if (theEvent.preventDefault)
				theEvent.preventDefault();
		} else {
			$("#f_regist ." + classname + " .message").text("*opcional");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'yellow'
			});
		}

	});
	$("#regist #f_regist #ip_idd").keypress(function(e) {

		var classname = $(this).closest('tr').attr("class");

		var theEvent = e || window.event;
		var key = theEvent.keyCode || theEvent.which;

		key = String.fromCharCode(key);

		if (theEvent.keyCode === 8) {
			return;
		}
		var regex = /[0-9]|\./;
		if (!regex.test(key)) {
			$("#f_regist ." + classname + " .message").text("*Não são permitidas letras");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'red'
			});
			theEvent.returnValue = false;
			if (theEvent.preventDefault)
				theEvent.preventDefault();
		} else {
			$("#f_regist ." + classname + " .message").text("*opcional");
			$("#f_regist ." + classname + " .message").css({
				'color' : 'yellow'
			});
		}

	});
	$("#regist #f_regist .required input").blur(function() {
		var value = $(this).val();
		if (value == "") {
			$(this).animate({
				backgroundColor : 'rgba(255,0,0,.5)'
			});
		} else {
			$(this).animate({
				backgroundColor : 'white'
			});
		}
	});

	$("#regist #f_regist .submit").hover(function() {
		var ip_name = $("#regist #f_regist #ip_name").val();
		var ip_mail = $("#regist #f_regist #ip_mail").val();
		var ip_contact = $("#regist #f_regist #ip_contact").val();
		var file = $("#regist #f_regist #file").val();

		if (ip_name == "") {
			$("#regist #f_regist #ip_name").animate({
				backgroundColor : 'rgba(255,0,0,.5)'
			});
			isEmpty = true;
		} else {
			$("#regist #f_regist #ip_name").animate({
				backgroundColor : 'white'
			});
			isEmpty = false;
		}

		if (ip_mail == "") {
			$("#regist #f_regist #ip_mail").animate({
				backgroundColor : 'rgba(255,0,0,.5)'
			});
			isEmpty = true;
		} else {
			$("#regist #f_regist #ip_mail").animate({
				backgroundColor : 'white'
			});
			isEmpty = false;
		}

		if (ip_contact == "") {
			$("#regist #f_regist #ip_contact").animate({
				backgroundColor : 'rgba(255,0,0,.5)'
			});
			isEmpty = true;
		} else {
			$("#regist #f_regist #ip_contact").animate({
				backgroundColor : 'white'
			});
			isEmpty = false;
		}

		if (file == "") {
			$("#regist #f_regist #file").animate({
				backgroundColor : 'rgba(255,0,0,.5)'
			});
			isEmpty = true;
		} else {
			$("#regist #f_regist #file").animate({
				backgroundColor : 'white'
			});
			isEmpty = false;
		}
	});

	$("#regist #f_regist").submit(function() {
		if (isEmpty) {
			return false;
		}
	});
});

